﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BusinessLayer.Tables;

public partial class Admin_Poslar : ChildPage
{
    #region Function
    private void CreateArea()
    {
        foreach (System.Web.UI.Control c in pnlControls.Controls)
        {
            if (c is CrmControls.CrmTextBox)
            {
                CrmControls.CrmTextBox t = c as CrmControls.CrmTextBox;
                t.Text = "";
            }
            if (c is CrmControls.CrmDropDown)
            {
                CrmControls.CrmDropDown drop = c as CrmControls.CrmDropDown;
                if (drop.SelectedIndex != -1)
                    drop.SelectedIndex = 0;
            }
        }
    }
    void KayitBul(string id, string id2)
    {
        Db_Poslar pos = new Db_Poslar();
        pos.CashID = Convert.ToInt16(id);
        pos.CashDepoID = Int16.Parse(id2);
        pos.KayitBul();
        if (pos.CashDepoID >= 0)
        {
            txtCashAdi.Text = pos.CashAdi;
            drpcashdepoid.SelectedValue = pos.CashDepoID.ToString();
            txtCashFatseri.Text = pos.CashFatseri;
            txtCashID.Text = pos.CashID.ToString();
            txtCashMsg.Text = pos.CashMsg;
            drpProtNo.SelectedValue = pos.CashPorNo.ToString();
            txtCashSaleImp.Text = pos.CashSaleImp;
            txtCashStokExp.Text = pos.CashStockExp;
            drpCashTip.SelectedValue = pos.CashTip.ToString();
            txtCashYardimciPrg.Text = pos.CashYardimciPrg;
        }
    }
    private void Kaydet()
    {
        Db_Poslar pos = new Db_Poslar();
        DataTable toplam = pos.GetAllRecord_Datatable("pos_rec_id");
        if (toplam.Rows.Count < 2)
        {
            if (txtCashID.Text != "" && txtCashAdi.Text != "" && txtCashMsg.Text != "" && txtCashSaleImp.Text != "" && txtCashStokExp.Text != "")
            {
                pos.CashAdi = txtCashAdi.Text;
                if (drpcashdepoid.SelectedIndex != -1)
                    pos.CashDepoID = Int16.Parse(drpcashdepoid.SelectedItem.Value);
                if (txtCashFatseri.Text != "")
                    pos.CashFatseri = txtCashFatseri.Text;
                else
                    pos.CashFatseri = string.Empty;
                pos.CashID = int.Parse(txtCashID.Text);
                pos.CashMsg = txtCashMsg.Text;

                //if (drpCashNakitKasa.SelectedIndex != -1)
                pos.CashNakitKasa = ""; // drpCashNakitKasa.SelectedItem.Value;
                if (drpProtNo.SelectedIndex!= -1)
                    pos.CashPorNo = int.Parse(drpProtNo.SelectedItem.Value);
                pos.CashSaleImp = txtCashSaleImp.Text;
                pos.CashStockExp = txtCashStokExp.Text;

                if (drpCashTip.SelectedIndex!= -1)
                    pos.CashTip = Int16.Parse(drpCashTip.SelectedItem.Value);
                if (txtCashYardimciPrg.Text != "")
                    pos.CashYardimciPrg = txtCashYardimciPrg.Text;
                else
                    pos.CashYardimciPrg = string.Empty;
                pos.Create_Date = DateTime.Now;
                pos.Create_User = 1;
                pos.Hidden = true;
                pos.Locked = true;
                pos.Update_Date = DateTime.Now;
                pos.Update_User = 1;
                pos.Save();
                CreateArea();
            }
            else
            {
                if (txtCashID.Text == "")
                    CrmControls.MessageBx.Show("Kasa Kodu Boş Bırakılmaz...");
                else if (txtCashAdi.Text == "")
                    CrmControls.MessageBx.Show("Kasa Adı Boş Bırakılmaz...");
                else if (txtCashStokExp.Text == "")
                    CrmControls.MessageBx.Show("Stok Yolu Boş Bırakılmaz...");
                else if (txtCashSaleImp.Text == "")
                    CrmControls.MessageBx.Show("Satış Dosya Yolu Boş Bırakılmaz...");
                else if (txtCashMsg.Text == "")
                    CrmControls.MessageBx.Show("Mesaj Yolu Boş Bırakılmaz...");

            }
        }
        else
            CrmControls.MessageBx.Show("Yazar Kasa Sayısı 2'den Fazla Olamaz!");
    }
    void DropDoldur()
    {
        Db_Depo depo = new Db_Depo();
        DataTable dt = depo.GetAllRecord_Datatable("depo_id ", "depo_adi ");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            ListItem lst = new ListItem();
            lst.Value = dt.Rows[i][0].ToString();
            lst.Text = dt.Rows[i][1].ToString();
            drpcashdepoid.Items.Add(lst);
        }

    }
    private void AttributeAdd()
    {
        imgbtnKaydetKapat.Attributes.Add("onclick", "return Kontrol();");
        imgbtnKaydet.Attributes.Add("onclick", "return Kontrol();");
        imgbtnSil.Attributes.Add("onclick", "return SilKontrol();");
    }
    #endregion
    protected void Page_Load(object sender, EventArgs e)
    {
        AttributeAdd();
        DropDoldur();
        if (!IsPostBack)
        {
            if (Request.QueryString.Count > 0)
            {
                string id = Request.QueryString["id"];
                string id2 = Request.QueryString["id2"];
                KayitBul(id, id2);
            }
        }
    }
    protected void imgbtnKaydet_Click(object sender, ImageClickEventArgs e)
    {
        Kaydet();
        this.ClientScript.RegisterStartupScript(typeof(Page), "Yenile", "Refresh();", true);
    }
    protected void imgbtnKaydetKapat_Click(object sender, ImageClickEventArgs e)
    {
        Kaydet();
        this.Page.ClientScript.RegisterStartupScript(typeof(Page), "test", "PageClose();", true);
    }
    protected void imgbtnSil_Click(object sender, ImageClickEventArgs e)
    {
        Db_Poslar pos = new Db_Poslar();
        if (txtCashID.Text != "")
        {
            pos.CashID = int.Parse(txtCashID.Text);
            if (pos.CashID != 0)
            {
                pos.KayitSil();
                CreateArea();
            }
            else
                CrmControls.MessageBx.Show("Lütfen Kasa Kod Bölümüne Silmek İstediğiniz Kasa Kodunu Girin!..");
        }
        this.ClientScript.RegisterStartupScript(typeof(Page), "Tazele", "Refresh();", true);
    }
    protected void imgNewREcord_Click(object sender, ImageClickEventArgs e)
    {
        CreateArea();
    }
}
